package com.thientan.controller

import java.text.SimpleDateFormat

import com.thientan.model.HopDong
import com.thientan.model.NhanVien
import com.thientan.model.ThongKePhieuThu
import com.thientan.model.enums.LoaiNhanVien
import com.thientan.model.enums.TrangThaiHopDong
import com.thientan.service.ThongKeGiaoPhieuThuService
import com.thientan.utils.Constants
import com.thientan.utils.Utility

class ThongKeGiaoPhieuThuController extends AbstractBaseController {
	ThongKeGiaoPhieuThuService thongKeGiaoPhieuThuService

	@Override
	public Class getDomainClass() {
		return ThongKePhieuThu.class
	}

	@Override
	public String getDomainName() {
		message(code : "default.thientan.messages.thongKe")
	}

	@Override
	public Object getDomainService() {
		return thongKeGiaoPhieuThuService
	}

	@Override
	public String getDisplayFieldName() {
		null
	}

	@Override
	protected String getExportFileName() {
		"ThongKeGiaoNhanPhieuThu"
	}
		
	protected Map buildExportData(Map exportParams){
		def sdfQueryDate = new SimpleDateFormat(Constants.TO_DATE_FORMAT)
		def sdfDateTo = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT)
		Date fromDate = params["fromDate"] != "" ? sdfQueryDate.parse(params["fromDate"]) : null
		Date toDate = params["toDate"] != "" ? sdfQueryDate.parse(params["toDate"]) : null
		Date toDateForQuery = Utility.increaseDays(toDate, 1)
		String sql = "select hd.nvThuNgan.id, substring(hd.maHd, 1, 3), hd.tienConLai from HopDong hd " + 
					 "where hd.trangThai <> '${TrangThaiHopDong.DONG.value}' and hd.nvThuNgan is not null " + 
					 "and hd.daGiaoPhieu != null and hd.daGiaoPhieu >= '${sdfQueryDate.format(fromDate)}' and hd.daGiaoPhieu < '${sdfQueryDate.format(toDateForQuery)}'"
		List hds = HopDong.executeQuery(sql)
		
		List thuNgans = NhanVien.withCriteria {
			projections {
				property("id")
				property("maNv")
				property("hoTen")
			}
			eq("loaiNhanVien", LoaiNhanVien.THU_NGAN)			
			eq("kichHoat", true)
		}

		List thuNganData = []
		thuNgans.each {
			Long thuNganId = it[0] as Long
			String maTn = it[1]
			String tenTn = it[2].substring(it[2].lastIndexOf(" ") + 1)
			List hopDongOfTn = hds.findAll {hd -> (hd[0] as Long) == thuNganId}

			Map maHd2ThuNganInfo = [:]
			hopDongOfTn.each {
				String maHd = it[1]
				Double tienPhaiThu = it[2]
				Map thuNganInfo = maHd2ThuNganInfo.get(maHd)
				if (thuNganInfo) {
					thuNganInfo["soPhieu"] = thuNganInfo["soPhieu"] + 1
					thuNganInfo["soTien"] = thuNganInfo["soTien"] + tienPhaiThu
				} else {
					maHd2ThuNganInfo.put(maHd, ["soPhieu" : 1, "soTien" : tienPhaiThu])
				}
			}
			
			maHd2ThuNganInfo.keySet().each {
				Map phieuThuInfo = maHd2ThuNganInfo.get(it)
				thuNganData << ["maHd" : it, "maTn" : maTn, "tenTn" : tenTn, "soPhieu" : phieuThuInfo["soPhieu"], "soTien" : phieuThuInfo["soTien"]]
			}
			
			// Remove hopDong already checked.
			hds.removeAll(hopDongOfTn)
		}
		
		return [
			data : thuNganData,
			"ngayXuat" : sdfDateTo.format(new Date()),
			"thoiGian" : (fromDate ? sdfDateTo.format(fromDate) : "") + " - " + (toDate ? sdfDateTo.format(toDate) : ""),
			"tongPhieu" : Utility.moneyFormat(thuNganData ? thuNganData.sum{it["soPhieu"]} : 0),
			"tongTien" : Utility.moneyFormat(thuNganData ? thuNganData.sum{it["soTien"]} : 0)
		]
	}
}
